import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Creater with IntelliJ IDEA.
 * Description:
 * User: 86152
 * Dae: 2022-07-28
 * Time: 14:32
 */
//使用库提供的阻塞队列
public class demo1 {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<Integer>();
        //消费者模型
        Thread customer = new Thread() {
            public void run() {
                int value;
                while(true) {
                    try {
                        value = blockingQueue.take();
                        System.out.println("消费！"+value);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        customer.start();
        Thread provider = new Thread() {
            public void run() {
                int n=1;
                while(true) {
                    try {
                        blockingQueue.put(n);
                        System.out.println("生产！"+n);
                        n++;
                        sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        provider.start();
    }
}
